//
//  XGRegisterViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/4/9.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import MBProgressHUD

class XGRegisterViewController: UIViewController
{
    // MARK: - 控制器生命周期方法
        
    override func loadView()
    {
        super.loadView()
        
        setUpUI()
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        navigationItem.title = "注册"
        NotificationCenter.default.addObserver(self, selector: #selector(textDidChangeNotification), name: UITextField.textDidChangeNotification, object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
    {
        view.endEditing(true)
    }
    
    // MARK: - 私有属性
    
    /// 用户名
    private lazy var userNameTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "请输入用户名"
        textField.clearButtonMode = .whileEditing
        textField.borderStyle = .roundedRect
        textField.font = UIFont.systemFont(ofSize: 17)
        return textField
    }()
    /// 手机号
    private lazy var phoneNumberTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "请输入手机号"
        textField.clearButtonMode = .whileEditing
        textField.borderStyle = .roundedRect
        textField.font = UIFont.systemFont(ofSize: 17)
        textField.keyboardType = .phonePad
        return textField
    }()
    /// 邮箱
    private lazy var emailTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "请输入邮箱"
        textField.clearButtonMode = .whileEditing
        textField.borderStyle = .roundedRect
        textField.font = UIFont.systemFont(ofSize: 17)
        textField.keyboardType = .emailAddress
        return textField
    }()
    /// 密码
    private lazy var passwordTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "请输入密码"
        textField.clearButtonMode = .whileEditing
        textField.borderStyle = .roundedRect
        textField.font = UIFont.systemFont(ofSize: 17)
        textField.isSecureTextEntry = true
        return textField
    }()
    /// 确认密码
    private lazy var confirmPasswordTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "确认密码"
        textField.clearButtonMode = .whileEditing
        textField.borderStyle = .roundedRect
        textField.font = UIFont.systemFont(ofSize: 17)
        textField.isSecureTextEntry = true
        return textField
    }()
    /// 注册按钮
    private lazy var registerButton: UIButton = { [weak self] in
        let button = UIButton(title: "注册", fontSize: 20, normalColor: UIColor.white, highlightedColor: UIColor.white, target: self, action: #selector(registerAction))
        button.setBackgroundImage(UIColor.colorWithHexString(hexadecimal: "#FF6A6A").toImage(), for: .normal)
        button.setBackgroundImage(UIColor.colorWithHexString(hexadecimal: "#FFAEB9").toImage(), for: .disabled)
        button.isEnabled = false
        button.layer.cornerRadius = 20
        button.layer.masksToBounds = true
        return button
    }()
    private let viewModel = XGRegisterViewModel()
}

private extension XGRegisterViewController
{
    /// 注册账号事件
    @objc func registerAction() -> Void
    {
        view.endEditing(true)
        
        MBProgressHUD.showProgressHUD(text: "注册中...")
        viewModel.registerUser(screenName: userNameTextField.text!, phoneNumber: phoneNumberTextField.text!,email: emailTextField.text!, password: passwordTextField.text!, confirmPassword: confirmPasswordTextField.text!) { (isSuccess, message) in
            if isSuccess {
                MBProgressHUD.showSuccessTips(text: message)
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    self.navigationController?.popViewController(animated: true)
                }
            } else {
                MBProgressHUD.showErrorTips(text: message)
            }
        }
    }
    
    /// textField通知监听方法
    @objc func textDidChangeNotification() -> Void
    {
        registerButton.isEnabled = userNameTextField.hasText &&
            phoneNumberTextField.hasText &&
            emailTextField.hasText &&
            passwordTextField.hasText &&
            confirmPasswordTextField.hasText
    }
    
    func setUpUI() -> Void
    {
        view.backgroundColor = UIColor.white
        view.addSubview(userNameTextField)
        view.addSubview(phoneNumberTextField)
        view.addSubview(emailTextField)
        view.addSubview(passwordTextField)
        view.addSubview(confirmPasswordTextField)
        view.addSubview(registerButton)
        
        userNameTextField.snp.makeConstraints { (make) in
            make.top.equalTo(view.snp.topMargin).offset(30)
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(44)
        }
        phoneNumberTextField.snp.makeConstraints { (make) in
            make.top.equalTo(userNameTextField.snp.bottom).offset(30)
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(44)
        }
        emailTextField.snp.makeConstraints { (make) in
            make.top.equalTo(phoneNumberTextField.snp.bottom).offset(30)
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(44)

        }
        passwordTextField.snp.makeConstraints { (make) in
            make.top.equalTo(emailTextField.snp.bottom).offset(30)
            make.left.right.height.equalTo(phoneNumberTextField)
            make.height.equalTo(44)
        }
        confirmPasswordTextField.snp.makeConstraints { (make) in
            make.top.equalTo(passwordTextField.snp.bottom).offset(30)
            make.left.right.height.equalTo(phoneNumberTextField)
            make.height.equalTo(44)
        }
        registerButton.snp.makeConstraints { (make) in
            make.top.equalTo(confirmPasswordTextField.snp.bottom).offset(30)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: ceil(TodayNews_Screen_Width * 0.6), height: 44))
        }
    }
}
